var tar_module = "libc.so"

function inject_jnitrace() {
    var so_path = ''
    var hook_so
    if (Process.arch === "arm64") {
        so_path = "/data/app/libnative_hook64.so"
        hook_so = Process.findModuleByName('libnative_hook64.so')
        Module.load("/data/app/libdobby64.so")
    } else if (Process.arch === "arm") {
        so_path = "/data/app/libnative_hook.so"
        hook_so = Process.findModuleByName('libnative_hook.so')
        Module.load("/data/app/libdobby.so")
    }

    if (hook_so == null)
        hook_so = Module.load(so_path)
    console.log("jnitrace.so: " + hook_so)
    let init_hook_addr = hook_so.findExportByName('init_jni_hook')
    console.log("init_jni_hook: " + init_hook_addr)
    var init_hook = new NativeFunction(init_hook_addr, 'void', ['pointer', 'pointer'])
    console.log("init_jni_hook js: " + init_hook)
//hook的so
    let target_name = tar_module

    let so_path_native_addr = Memory.alloc(target_name.length + 2)
    so_path_native_addr.writeUtf8String(target_name)
    console.log("target module: " + so_path_native_addr.readCString())

    let jnienv = Java.vm.getEnv().handle
    init_hook(jnienv, so_path_native_addr)
    console.log("finish hook!")
}

function hook_android_dlopen_ext(modeul_name) {
    var android_dlopen_ext = Module.findExportByName(null, "android_dlopen_ext")
    console.log(" android_dlopen_ext:" + android_dlopen_ext);
    Interceptor.attach(android_dlopen_ext, {
        onEnter: function (args) {
            this.file_name = this.context.r0.readCString();
        },
        onLeave: function (retval) {
            if (this.file_name.indexOf(modeul_name) >= 0) {
                console.log("loaded : " + this.file_name)
                inject_jnitrace()
            }
        },
    })
}

//frida -U -f com.xingin.xhs -l ./agent/inject_jnitrace/jnitrace.js --no-pause

hook_android_dlopen_ext(tar_module)